clear all 
set more off
* ---------------------------------------------------------------------------
* This code creates Figure 2. It plots exit by formality status
* ---------------------------------------------------------------------------


* graph colors and sizes
set autotabgraphs on

local msize1 "medsmall"
local msize2 "medlarge"

local color1 "0 76 167"
local color2 "146 0 0"
local color3 "62 150 81"
local colorgb "white"

local color1b "0 76 167"
local color2b "146 0 0"
local color3b "62 150 81"

* load
use "Transitions_Formal.dta", clear

* set countries that you are willing to assume have aggregate exit = formal exit
gen pickout = 1 if ccode == "USA" | ccode == "FRA" | ccode == "GBR" | ccode == "CHE"

* drop everyone who doesn't have formality status 
drop if pickout != 1 & hasinformal != 1

tostring(year), replace
gen cyid = ccode + "--" + year
destring(year), replace
order cyid, after(year)


* for each country where aggregate = formal,
* create a new row for formal exit.

vallist(cyid) if pickout == 1, local(cplist)
foreach x of local cplist {
	
	local new = _N + 1
	set obs `new'

	gen _hold1 = ytrate if cyid == "`x'" & formal == 2
	egen _hold2 = max(_hold1)

	gen _hold3 = AGDP if cyid == "`x'" & formal == 2
	egen _hold4 = max(_hold3)	

	replace trans = "WM" if cyid == ""
	replace pickout = 1 if cyid == ""
	replace AGDP = _hold4 if cyid == ""
	replace formal = 1 if cyid == ""
	replace lcgdp = ln(AGDP) if missing(lcgdp)
	replace cyid = "`x'" if cyid == ""

	replace ytrate = _hold2 if cyid == "`x'" & formal == 1

	drop _hold1 _hold2 _hold3 _hold4

}


* compute some country averages
bys ccode formal: egen xmean = mean(ytrate)
bys ccode formal: egen gdpmean = mean(AGDP)
bys ccode formal: gen counter = _n

local ylist "M"
foreach y of local ylist {

	* formal
	reg ytrate lcgdp if trans == "W`y'" & formal == 1 & ytrate > 0 
	predict trend_f

	* aggregate 
	reg ytrate lcgdp if trans == "W`y'" & formal == 2 & ytrate > 0 
	predict trend_agg


	* figure 2: formal and aggregate
	gsort+ AGDP
	
	#delimit ;
	twoway
	(scatter ytrate AGDP if trans == "W`y'" & formal == 1 & pickout != 1, mcolor("`color1b'%10") msize(`msize1') msymbol(O) mlwidth(none)) 
	(scatter ytrate AGDP if trans == "W`y'" & formal == 2 & pickout != 1, mcolor("`color2b'%10") msize(`msize1') msymbol(D) mlwidth(none)) 

	(scatter ytrate AGDP if trans == "W`y'" & formal == 2 & pickout == 1, mcolor("`color2b'%10") msize(`msize1') msymbol(Dh) mlwidth(none)) 

	(line trend_f AGDP if trans == "W`y'" & formal == 1, lcolor("`color1'%50") lwidth(medthin) alc(background)) 
	(line trend_agg AGDP if trans == "W`y'" & formal == 2, lcolor("`color2'%50") lwidth(medthin) alc(background) lpattern(dash)) 	

	(scatter xmean gdpmean if counter == 1 & trans == "W`y'" & formal == 1 & pickout != 1, mcolor("`color1'") msize(`msize2') mlabel(ccode) msymbol(O) mlabcolor("`color1'")) 	
	(scatter xmean gdpmean if counter == 1 & trans == "W`y'" & formal == 2 & pickout != 1, mcolor("`color2'") msize(`msize2') mlabel(ccode) msymbol(D) mlabcolor("`color2'")) 
	(scatter xmean gdpmean if counter == 1 & trans == "W`y'" & formal == 2 & pickout == 1, mcolor("`color2'") msize(`msize2') mlabel(ccode) msymbol(Dh) mlabcolor("`color2'")) 		
	, legend(order(6 4 7 5 8) label(6 "formal") label(7 "aggregate") label(8 "aggregate (informal unknown)") label(4 "fitted") label(5 "fitted") col(2)) 
	name(w`y'_aggformal_`loopi')
	graphregion(color("`colorgb'")) plotregion(color(none)) bgcolor(none) xtitle("GDP per capita") 
	yscale(r(0 0.20)) ylabel(0(0.05)0.20) xscale(log) xlabel(4000 8000 16000 32000 64000);			
	#delimit cr

	graph export "Figures/Figure2.tif", replace
	graph export "Figures/pdf_figures/Figure2.pdf", replace

	drop trend_*


	
}
*/


